﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.Extend.Dtos.WoekLog;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller.Extend
{
    /// <summary>
    /// 工作日志
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.28 
    /// </summary>
    [Route("api/Extend/[controller]")]
    public class WorkLogController : BaseApiController
    {
        private WorkLogBll workLogBll = new WorkLogBll();
        private UserBll userBll = new UserBll();

        #region Get
        /// <summary>
        /// 列表(我发出的)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("Send")]
        public async Task<ResponseResult> GetSendList([FromQuery] PageInput input)
        {
            var data = await workLogBll.GetSendList(input);
            var output = new PageResult();
            output.pagination = data.pagination;
            output.list = data.list.MapToList<WorkLogListOutput>();
            return Success(output);
        }

        /// <summary>
        /// 列表(我收到的)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("Receive")]
        public async Task<ResponseResult> GetReceiveList([FromQuery] PageInput input)
        {
            var data = await workLogBll.GetReceiveList(input);
            var output = new PageResult();
            output.pagination = data.pagination;
            output.list = data.list.MapToList<WorkLogListOutput>();
            return Success(output);
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var output = (await workLogBll.GetInfo(id)).MapTo<WorkLogInfoOutput>();
            output.userIds = output.toUserId;
            output.toUserId = userBll.GetUserName(output.toUserId);
            return Success(output);
        }
        #endregion

        #region Post
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Creater([FromBody] WorkLogCrInput input)
        {
            var entity = input.MapTo<WorkLogEntity>();
            await workLogBll.Create(entity);
            return Success("新建成功");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] WorkLogUpInput input)
        {
            var entity = input.MapTo<WorkLogEntity>();
            await workLogBll.Update(id, entity);
            return Success("修改成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await workLogBll.GetInfo(id);
            await workLogBll.Delete(entity);
            return Success("删除成功");
        }
        #endregion
    }
}
